package diploma.kisel.modelas.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Domain class for Device (table - device).
 * 
 * @author Nikolay Kisel
 *
 */
@Entity
@Table(name = "device")
@NamedQueries({
		@NamedQuery(name = "Device.findAll", query = "select d from Device d"),
		@NamedQuery(name = "Device.findByPrice", query = "select d from Device d where d.cost >= :minPrice and d.cost <= :maxPrice"),
		@NamedQuery(name = "Device.findById", query = "select d from Device d where d.id = :id"),
		@NamedQuery(name = "Device.findByPriceAndType", query = "select d from Device d where d.cost >= :minPrice and d.cost <= :maxPrice and d.deviceType = :type")})
public class Device {

	private Integer id;
	private String number;
	private String brand;
	private String features;
	private Integer cost;

	// for many to many mapping
	private Set<Workstation> workstations = new HashSet<Workstation>();
	// for many to one mapping
	private DeviceType deviceType;

	public Device() {

	}

	public Device(String number, String brand, Integer cost, String features) {
		this.number = number;
		this.brand = brand;
		this.cost = cost;
		this.features = features;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id_device")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "number")
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Column(name = "brand")
	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	@Column(name = "cost")
	public Integer getCost() {
		return cost;
	}

	public void setCost(Integer cost) {
		this.cost = cost;
	}

	@Column(name = "features")
	public String getFeatures() {
		return features;
	}

	public void setFeatures(String features) {
		this.features = features;
	}

	@ManyToMany
	@JoinTable(name = "list_device", joinColumns = @JoinColumn(name = "id_device"), inverseJoinColumns = @JoinColumn(name = "id_workstation"))
	public Set<Workstation> getWorkstations() {
		return workstations;
	}

	public void setWorkstations(Set<Workstation> workstations) {
		this.workstations = workstations;
	}

	@ManyToOne
	@JoinColumn(name = "id_device_type")
	public DeviceType getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(DeviceType deviceType) {
		this.deviceType = deviceType;
	}	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Device other = (Device) obj;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Device [id=" + id + ", number=" + number + ", brand=" + brand
				+ ", features=" + features + ", cost=" + cost + ", type="
				+ deviceType + "]";
	}

}
